# -*- coding:utf-8 -*-

import articleDao, json, os, time, hashlib
os.sys.path.append('../comment/')
import commentLogic
import os
os.sys.path.append('../../utils/constants/')
from constants import *

class articleLogic(object):
    "article: let me tell u sth about storage-visiting"
    def __init__(self, dbHandler, memcache_conn):
        self.__dao = articleDao.articleDao(dbHandler)
        self.__mc = memcache_conn
        self.__mc_expire_time = 10*86400

        dict_comment_table = {}
        dict_comment_table['thread_id_keyname'] = 'article_id'
        dict_comment_table['thread_table'] = 'article'
        dict_comment_table['comment_table'] = 'article_comment'
        dict_comment_table['comment_id_table'] = 'article_comment_id'
        self.__commentLogic = commentLogic.commentLogic(dict_comment_table, dbHandler, memcache_conn)

    def remove(self, delete_item):
        return self.__dao.remove(delete_item)
    def add(self, article_item):
        return self.__dao.add(article_item)

    def mod(self, article_item):
        return self.__dao.mod(article_item)

    def like(self, like_item):
        # like_item has 'article_id', 'uip', 'like'
        ret = {}
        ret['retcode'] = 0
        ret['message'] = "OK"
        # 检查合法性
        if not like_item.has_key('article_id') or not like_item.has_key('uip'):
            ret['retcode'] = E_KEY_NOT_EXIST
            ret['message'] = "like_item field [%s] or [%s] not exist" % ('article_id', 'uip', )
            return ret
        
        key = 'like:article:%u:%s' % (like_item['article_id'], like_item['uip'],)
        md5str = hashlib.md5(key).hexdigest()
        mckey = md5str[:16]
        if not self.__mc.get(mckey):
            self.__mc.add(mckey, int(time.time()), self.__mc_expire_time)
            return self.__dao.like(like_item)
        else:
            ret = {}
            ret['retcode'] = E_DUP_OPERATION
            ret['message'] = 'like duplicate operation table[%s] uip[%s] article_id[%u].' %\
                    ('article', like_item['uip'], like_item['article_id'],)
            return ret

    def clear_today_pv(self):
        return self.__dao.clear_today_pv()
    def get_detail(self, article_id, uip):
        pv_item = {}
        pv_item['article_id'] = article_id
        pv_item['uip'] = uip
        self.add_pv_count(pv_item)

        ret = {}
        ret['retcode'] = 0
        ret['message'] = "OK"
        ret['retbody'] = self.__dao.get_detail(article_id)
        if ret['retbody'] and ret['retbody'].has_key('content'):
            ret['retbody']['content'] = json.loads(ret['retbody']['content'])
        else:
            ret['retcode'] = E_DATA_ACCESS_ERROR
            ret['message'] = "article_id : %u access fail." % (article_id,)
        return ret
    def add_pv_count(self, pv_item):
        # pv_item has 'article_id', 'uip',
        # ip一小时内pv去重
        ret = {}
        ret['retcode'] = 0
        ret['message'] = "OK"
        # 检查合法性
        if not pv_item.has_key('article_id') or not pv_item.has_key('uip'):
            ret['retcode'] = E_KEY_NOT_EXIST
            ret['message'] = "pv_item field [%s] or [%s] not exist" % ('article_id', 'uip', )
            return ret
        key = 'pv:article:%u:%s:%s' % (pv_item['article_id'], pv_item['uip'], time.strftime("%Y%m%H"), )
        md5str = hashlib.md5(key).hexdigest()
        mckey = md5str[:16]
        if not self.__mc.get(mckey):
            self.__mc.add(mckey, int(time.time()), self.__mc_expire_time)
            return self.__dao.add_pv_count(pv_item['article_id'])
        else:
            ret = {}
            ret['retcode'] = E_DUP_OPERATION
            ret['message'] = 'pv duplicate operation table[%s] uip[%s] article_id[%u].' %\
                    ('article', pv_item['uip'], pv_item['article_id'],),
            return ret

    def order_by(self, field, itype, pageno, desc = True):
        ret = {}
        ret['retcode'] = 0
        ret['message'] = "OK"
        ret['retbody'] = self.__dao.order_by(field, itype, pageno, desc)
        return ret

    def ding_comment(self, ding_item):
        # ding_item has 'thread_id', 'comment_id', 'uip'
        return self.__commentLogic.ding_comment(ding_item)
    def add_comment(self, comment_item):
        return self.__commentLogic.add_comment(comment_item)
    def del_comment(self, comment_item,):
        return self.__commentLogic.del_comment(comment_item)
    def get_comment(self, thread_id, pageno):
        return self.__commentLogic.get_comment(thread_id, pageno)
